/**
 * Copyright (c) 2013, FPX and/or its affiliates. All rights reserved.
 * Use, Copy is subject to authorized license.
 */
package com.fpx.open.platform.common.util;

import java.security.MessageDigest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fpx.open.platform.domain.ae.response.Response;
import com.fpx.open.platform.domain.track.TracesElement;
import com.fpx.open.platform.domain.track.TracesList;

import sun.misc.BASE64Encoder;

/**
 * 速卖通，物流宝接口，将数据发给淘宝
 * 
 * @author dranson
 * @date 2013年11月5日
 */
public abstract class TaobaoHelper {

    public static Response getResponseByValidatError(String errorInfo, String sendData) {
        Response resp = new Response();
        resp.setSuccess(false);
        resp.setReason("A003");
//        resp.setBackData(errorInfo);
//        resp.setSendData(sendData);
        return resp;
    }

    public static String md5Base64(String str) {
        String encodeStr = "";
        try {
            byte[] utfBytes = str.getBytes("UTF-8");
            MessageDigest mdTemp = MessageDigest.getInstance("MD5");
            mdTemp.update(utfBytes);
            byte[] md5Bytes = mdTemp.digest();
            BASE64Encoder b64Encoder = new sun.misc.BASE64Encoder();
            encodeStr = b64Encoder.encode(md5Bytes);
        } catch (Exception e) {
            return encodeStr;
        }
        return encodeStr;
    }

    private static String postServer(String msgType, String warehouseCode, String digestStr, String xmlData) {
        String url = "http://pac.partner.taobao.net/gateway/express_message_receiver.do?_input_charset=utf-8";
//    	String url = "http://pac.partner.taobao.com/gateway/express_message_receiver.do?_input_charset=utf-8";
        Map<String, String> params = new HashMap<String, String>();
        params.put("logistics_interface", xmlData);
       // params.put("logistic_provider_id", "FPXSZ");
        params.put("logistic_provider_id", warehouseCode);
        params.put("msg_type", msgType);
        // params.put("data_digest", md5Base64(xmlData + CommonParam.getStringByResource("taobao.token")));
        params.put("data_digest", md5Base64(xmlData + digestStr));
        params.put("version", "2.1");
        try {
            String xml = HTTPClient.sendPostRequest(url, params);
            return xml;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    
    /**
     * 跟踪信息由物流公司发送给淘宝
     * 
     * @param tracesList
     * @return
     */
    public static String tracepush(TracesList tracesList, String warehouseCode, String digestStr) {
        String sendData = JaxbUtil.marshal(tracesList, tracesList.getClass()); 
        // TODO
        sendData = sendData.replace("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>", "");
        
        List<TracesElement> els = tracesList.getTracesElements();
        for (TracesElement el : els) {
            if (el.getTxLogisticID() != null) {
                if (el.getMailNos().split(",").length != 1) {
                    return "物流订单号存在时，运单号列表[mailNos]只能有1个===> " + sendData;
                }
            }
        }
System.out.println("Request =" + sendData);        
        return postServer("TRACEPUSH", warehouseCode, digestStr, sendData);
    }

}
